from django.shortcuts import render, redirect
from django.views import View

# Create your views here.
from author.models import Author
from book.models import Book
from publish.models import Publish
from book.page.Pagations import Pagination

def home(request):
    book_date = Book.objects.all()[:3]
    return render(request, 'home.html', locals())


def book_detail(request):

    book_date = Book.objects.all()
    page_num = request.GET.get("page", 1)
    all_count = book_date.count()
    page_obj = Pagination(current_page=page_num, all_count=all_count, per_page_num=5)
    sid=(int(page_num)-1)*5
    book_queryset_list = book_date[page_obj.start:page_obj.end]
    publish_date = Publish.objects.all()
    author_all = Author.objects.all()
    if request.method == "POST":
        data = request.POST
        book_id = data.get("book_id") or data.get("id")
        action = data.get("action")
        book_obj = Book.objects.filter(pk=book_id)
        if action == 'update':
            title = request.POST.get('title')
            price = request.POST.get('price')
            desc = request.POST.get('desc')
            publish_id = request.POST.get('publish_id')[0]
            author_obj=request.POST.getlist('author_id')
            book_obj.update(title=title, price=price, desc=desc, publish_id=publish_id)
            Book.objects.get(pk=book_id).author.set([i for i in author_obj])
        if action == 'del':
            book_obj.delete()
    if request.method == "GET":
        wd = request.GET.get("wd")
        url = f'https://www.baidu.com/s?ie=UTF-8&wd={wd}'
    return render(request, 'book_detail.html', locals())


class BookAdd(View):
    def get(self, request, *args, **kwargs):
        publish_all = Publish.objects.all()
        author_all = Author.objects.all()
        return render(request, 'book_add.html', locals())

    def post(self, request, *args, **kwargs):
        data = request.POST
        title = data.get('title')
        price = data.get('price')
        desc = data.get("summary")
        cover = data.get("cover")
        link = data.get("link")
        publish_id = data.get("publish_id")[0]
        author_id = request.POST.getlist("author_id")
        publish_obj = Publish.objects.get(pk=int(publish_id))
        book_obj = Book.objects.create(title=title, price=int(price), desc=desc, cover=str(cover), link=link,
                                       publish=publish_obj)
        book_obj.author.set([Author.objects.get(pk=int(id)) for id in author_id])
        return redirect('book_detail')




def get_page(request):
    book_queryset = Book.objects.all()
    page_num = request.GET.get("page", 1)
    all_count = book_queryset.count()
    page_obj = Pagination(current_page=page_num, all_count=all_count, per_page_num=5)
    book_queryset_list = book_queryset[page_obj.start:page_obj.end]
    return render(request, "B.html", locals())



from django import template

register = template.Library()

@register.filter
def pagination_counter(value, page_number):
    return value + page_number


from django import forms
class MyForm(forms.Form):
    username = forms.CharField(max_length=6, min_length=3, label="用户名",
                               error_messages={
                                   "max_length": "最大6位",
                                   "min_length": "最小3位",
                                   "required": "必填字段",
                               })
    def clean_username(self):
        username = self.cleaned_data.get("username")
        if "666" in username:
            self.add_error("username", "用户名不能包含666")
        return username
